package com.briup.jd2407cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.jd2407cms.domain.User;
import com.briup.jd2407cms.domain.extend.UserExtend;
import com.briup.jd2407cms.exception.ServiceException;
import com.briup.jd2407cms.result.Result;
import com.briup.jd2407cms.result.ResultCode;
import com.briup.jd2407cms.service.UserService;
import com.briup.jd2407cms.mapper.UserMapper;
import org.apache.tomcat.jni.Local;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author 王杰_
* @description 针对表【cms_user】的数据库操作Service实现
* @createDate 2024-11-20 09:09:57
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    UserMapper userMapper;
    @Override
    public User login(String username, String password) {
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getUsername,username).eq(User::getPassword,password);
        User user = userMapper.selectOne(userLambdaQueryWrapper);
        if (user==null){
            throw new ServiceException(ResultCode.USER_IS_NOT_EXIST);
        }
        return user;
    }

    @Override
    public void add(User user) {
        String username = user.getUsername();
        String password = user.getPassword();
        if (username==null||password==null) {
            throw new ServiceException(ResultCode.PARAM_IS_BLANK);
        }
        username = username.trim();
        password = password.trim();
        if ("".equals(username) || "".equals(password)) {
            throw new ServiceException(ResultCode.PARAM_IS_BLANK);
        }
        user.setUsername(username);
        user.setPassword(password);

        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername,username);
        User user1 = userMapper.selectOne(lqw);
        if (user1!=null){
            throw new ServiceException(ResultCode.USER_IS_EXIST);
        }
        user.setRegisterTime(LocalDateTime.now());
        userMapper.insert(user);
    }

    @Override
    public User queryById(Long userId) {
        if (userId==null){
            throw new ServiceException(ResultCode.PARAM_IS_BLANK);
        }
        User user = userMapper.selectById(userId);
        if (user==null){
            throw new ServiceException(ResultCode.USER_IS_NOT_EXIST);
        }
        return user;
    }

    @Override
    public IPage<UserExtend> query(Integer pageNum, Integer pageSize, String username, String status, Integer roleId, Integer isVip) {
        IPage<UserExtend> page = new Page<>(pageNum, pageSize);
        userMapper.queryByPageAndCondition(page,username,status,roleId,isVip);
        return page;
    }

    @Override
    public void setVip(Long id) {
        User user = userMapper.selectById(id);
        if (user==null){
            throw new ServiceException(ResultCode.USER_IS_NOT_EXIST);
        }
        if (user.getIsVip()==1){
            throw new ServiceException(ResultCode.PARAM_IS_INVALID);
        }
        user.setIsVip(1);
        LocalDateTime time = LocalDateTime.now().plusMonths(1);
        user.setExpiresTime(time);
        userMapper.updateById(user);

    }

    @Override
    public void deleteByBatch(List<Long> ids) {
        if (ids==null|| ids.isEmpty()){
            throw new ServiceException(ResultCode.PARAM_IS_BLANK);
        }
        if (ids.size()==1){
            userMapper.deleteById(ids.get(0));
        }else {
            userMapper.deleteBatchIds(ids);
        }
    }

    @Override
    public List<User> getAllUser() {
        return userMapper.selectList(null);
    }
}




